

from PyQt5.QtWidgets import *
from PyQt5.QtCore import QTimer, QTime
from myplot_ui import Ui_MainWindow
import random

import numpy as np
import pyqtgraph as pg


class MyPlot(QMainWindow, Ui_MainWindow):

    def __init__(self):
        super().__init__()
        self.setupUi(self)
        
        
        self.var_init()
        #self.graph_init()
        self.sig_init()
 
    def graph_init(self):
        self.layout = QVBoxLayout()
        self.curve_graph = pg.GraphicsWindow()
        #self.curve_graph.showGrid(True, True)

        x = np.linspace(-5*np.pi,5*np.pi,500)
        y1 = 0.5*np.sin(x)
        y2 = 2*np.cos(x)
        y3 = np.sinc(x)
        y4 = np.sinc(x)

        self.plot1 = self.curve_graph.addPlot(left="Amplitude",bottom="x",title="y1,y4")
        self.curve_graph.nextRow()
        plot2 = self.curve_graph.addPlot(left="Amplitude",bottom="x",title="y2")
        self.curve_graph.nextRow()
        plot3 = self.curve_graph.addPlot(left="Amplitude",bottom="x",title="y3")
        for p,y,pen in zip([self.plot1,plot2,plot3],[y1,y2,y3],['r','g','y']):
            p.plot(x,y,pen=pen)
            p.showGrid(x=True,y=True)
            p.setRange(xRange=[-5*np.pi,5*np.pi],yRange=[-2.3,2.3],padding=0)

        square_wave_x = list(range(0,100))
        square_wave_y_1 = [random.randint(0,10) for i in range(100)]
        square_wave_y_2 = []
        for i in square_wave_y_1:
            if i>5:
                square_wave_y_2.append(1)
            else:
                square_wave_y_2.append(0)
        #print(square_wave_x)
        #print(square_wave_y_2)
        self.square_wave = pg.GraphicsWindow()
        self.square_wave_plot1 = self.square_wave.addPlot(left="Amplitude",bottom="x",title="y1")
        for p,y,pen in zip([self.square_wave_plot1],[y1],['r']):
            p.plot(square_wave_x,square_wave_y_2,pen=pen)
            p.showGrid(x=True,y=True)
            p.setRange(xRange=[0,1000],yRange=[0,1000],padding=0)


        self.layout.addWidget(self.curve_graph)
        self.layout.addWidget(self.square_wave)
        self.widget.setLayout(self.layout)

        self.timer = QTimer(self)
        self.timer.timeout.connect(self.auto_data_update)

    # 创建曲线图
    def create_curve_graph(self):
        pass

    # 创建方波图
    def create_square_wave(self):
        pass

    # 信号和槽的连接
    def var_init(self):  
        self.cnt = 0

    # 信号和槽的连接
    def sig_init(self):  
        #创建线程自动生产数据，显示波形
        self.btn1.clicked.connect(self.start_auto_draw)
        self.btn2.clicked.connect(self.b2_test)
        self.btn3.clicked.connect(self.b3_test)
        
    def b3_test(self):
        X = list(range(0,100))
        Y = list(range(0,100))
        self.square3 = pg.GraphicsWindow()
        self.square_wave_plot1 = self.square3.addPlot(left="Amplitude",bottom="x",title="y1")
        self.square_wave_plot1.plot(X,Y)
        self.square_wave_plot1.showGrid(x=True,y=True)
        self.square_wave_plot1.setRange(xRange=[0,120],yRange=[0,120],padding=0)

    def b2_test(self):
        square_wave_x = list(range(0,100))
        square_wave_y_1 = [random.randint(0,10) for i in range(100)]
        square_wave_y_2 = []
        for i in square_wave_y_1:
            if i>5:
                square_wave_y_2.append(1)
            else:
                square_wave_y_2.append(0)
        print(square_wave_x)
        print(square_wave_y_2)
        self.square2 = pg.GraphicsWindow()
        self.square_wave_plot1 = self.square2.addPlot(left="Amplitude",bottom="x",title="y1")
        self.square_wave_plot1.plot(square_wave_x,square_wave_y_2)
        self.square_wave_plot1.showGrid(x=True,y=True)
        self.square_wave_plot1.setRange(xRange=[0,120],yRange=[0,5],padding=0)

        self.layout.addWidget(self.square2)
        self.widget.setLayout(self.layout)

    def start_auto_draw(self):
        if (self.timer.isActive()):
            self.timer.stop()
            print("111")
        else:
            self.timer.start(1000)
            print("111222")

    def auto_data_update(self):
        self.cnt += 1
        x = np.linspace(0,self.cnt*np.pi,500)
        y = np.sin(x)
        self.plot1.plot(x,y,pen='r')


if __name__ == '__main__':
    app = pg.QtGui.QApplication([])

    myPlot = MyPlot()
    myPlot.show()

    app.exec_()